﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rolad : MonoBehaviour
{
    public MeshFilter meshFilter;
    public Transform A, B, C, D;
    public Transform M, N, O, P;

    public Transform K, L;
    public Transform E, R;

    public Transform O1, O2, O3;


    void Start()
    {
        A.position = TestAB(O1.position, O2.position, -0.5f, O1.position);
        B.position = TestAB(O1.position, O2.position, -0.5f, O2.position);

        C.position = TestAB(O1.position, O2.position, 0.5f, O2.position);
        D.position = TestAB(O1.position, O2.position, 0.5f, O1.position);

        O.position = TestAB(O2.position, O3.position, 0.5f, O3.position);
        P.position = TestAB(O2.position, O3.position, 0.5f, O2.position);

        N.position = TestAB(O2.position, O3.position, -0.5f, O3.position);
        M.position = TestAB(O2.position, O3.position, -0.5f, O2.position);


        K.position = GetLinesIntersection(A.position, B.position, N.position, M.position);
        L.position = GetLinesIntersection(C.position, D.position, O.position, P.position);

        E.position = TestAB(D.position, L.position, -1f, L.position);
        R.position = TestAB(O.position, L.position, 1f, L.position);

        //return;

        Mesh mesh = new Mesh();
        int l = 3;
        int vl = (l - 2) * 4 + 2 * 2;
        Vector3[] vertexs = new Vector3[vl];
        vertexs[0] = A.position;
        vertexs[1] = D.position;

        vertexs[2] = E.position;
        vertexs[3] = K.position;
        vertexs[4] = L.position;
        vertexs[5] = R.position;

        vertexs[6] = O.position;
        vertexs[7] = N.position;

        int tl = ((l - 2) * 2 + 2 * 2) * 3;
        Debug.Log(tl);
        int[] triangles = new int[tl];

        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 4;

        triangles[3] = 0;
        triangles[4] = 4;
        triangles[5] = 2;

        triangles[6] = 2;
        triangles[7] = 4;
        triangles[8] = 3;

        triangles[9] = 4;
        triangles[10] = 5;
        triangles[11] = 3;

        triangles[12] = 6;
        triangles[13] = 7;
        triangles[14] = 4;

        triangles[15] = 7;
        triangles[16] = 5;
        triangles[17] = 4;


        Vector3[] normals = new Vector3[vl];
        for (int i = 0; i < normals.Length; i++)
        {
            normals[i] = Vector3.up;
        }
        mesh.vertices = vertexs;
        mesh.triangles = triangles;
        mesh.normals = normals;
        meshFilter.mesh = mesh;
    }
    /// <summary>求平行线上的点</summary>
    /// <param name="m"></param>
    /// <param name="n"></param>
    /// <param name="am"></param>
    /// <param name="origin"></param>
    /// <returns></returns>
    public Vector2 TestAB(Vector2 m, Vector2 n, float am, Vector2 origin)
    {
        float mn = Vector2.Distance(n, m);
        float mo = n.x - m.x;
        float no = n.y - m.y;
        float ox = origin.x;
        float oy = origin.y;

        float ap = am * mo / mn;
        float pm = am * no / mn;

        return new Vector2(ox - pm, ap + oy);
    }
    /// <summary>求相交点</summary>
    /// <param name="o1"></param>
    /// <param name="o2"></param>
    /// <param name="o3"></param>
    /// <param name="o4"></param>
    public Vector2 GetLinesIntersection(Vector2 o1, Vector2 o2, Vector2 o3, Vector2 o4)
    {
        float x1 = o1.x;
        float x2 = o2.x;
        float x3 = o3.x;
        float x4 = o4.x;

        float y1 = o1.y;
        float y2 = o2.y;
        float y3 = o3.y;
        float y4 = o4.y;

        float x = (x1 * (y2 - y1) * (x4 - x3) - x3 * (y4 - y3) * (x2 - x1) + (y3 - y1) * (x4 - x3) * (x2 - x1)) / ((y2 - y1) * (x4 - x3) - (y4 - y3) * (x2 - x1));
        float y = (y1 * (x2 - x1) * (y4 - y3) - y3 * (x4 - x3) * (y2 - y1) + (x3 - x1) * (y4 - y3) * (y2 - y1)) / ((x2 - x1) * (y4 - y3) - (x4 - x3) * (y2 - y1));

        return new Vector2(x, y);
    }
}
